package com.itic.appbase.applications.redis.impl;

import java.util.Collection;
import java.util.List;
import java.util.concurrent.TimeUnit;

import javax.annotation.Resource;

import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Repository;

import com.itic.appbase.applications.redis.IRedisDao;


@Repository
public class RedisDao<K,V> implements IRedisDao<K, V>{

	@Resource
	private RedisTemplate<K, V> jedisTemplate;
	
	@Override
	public void unwatch() {
		
		this.jedisTemplate.unwatch();
	}

	@Override
	public boolean exec() {
		List<Object> result = this.jedisTemplate.exec();
		if(result!=null&&result.size()>0)
			return true;
		else
			return false;
	}

	@Override
	public void watch(K k) {
		this.jedisTemplate.watch(k);
	}
	
	@Override
	public void set(K k, V v) {
		this.jedisTemplate.opsForValue().set(k,v);
	}
	
	@Override
	public V get(K k) {
		return this.jedisTemplate.opsForValue().get(k);
	}

	@Override
	public void delete(K k) {
		this.jedisTemplate.delete(k);
	}

	@Override
	public void set(K k, V v, long timeout) {
		this.jedisTemplate.opsForValue().set(k, v, timeout, TimeUnit.SECONDS);
	}

	@Override
	public long getExpire(K k) {
		return this.jedisTemplate.getExpire(k);
	}

	@Override
	public void setExpire(K k, long timeout) {
		this.jedisTemplate.expire(k, timeout, TimeUnit.SECONDS);
	}

	@Override
	public void leftPush(K k, V v) {
		this.jedisTemplate.opsForList().leftPush(k, v);
	}
	
	@Override
	public void leftPush(K k, Collection<V> vs) {
		this.jedisTemplate.opsForList().leftPushAll(k, vs);
	}

	@Override
	public V rightPop(K k) {
		return this.jedisTemplate.opsForList().rightPop(k);
	}
	
	@Override
	public long listSize(K k) {
		return this.jedisTemplate.opsForList().size(k);
	}

}
